package com.ffapp.rainmonitorcustom.bean;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;

import com.ffapp.rainmonitorcustom.R;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.MPPointF;

import java.util.ArrayList;

import my.LogUtil;
import my.StringUtil;

public class MonitorAlarmNumBean extends BasisBean {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * processed : 19
	 * undispose : 170
	 * allnum : 189
	 */

	private int processed;
	private int undispose;
	private int allnum;

	public int getProcessed() {
		return processed;
	}

	public String getProcessPercentStr(){

	    if(allnum == 0)return "100%";
        LogUtil.i("getProcessPercentStr---->"+processed);
        LogUtil.i("getProcessPercentStr---->"+allnum);
        LogUtil.i("getProcessPercentStr---->"+ StringUtil.getPercent(processed,allnum));
	    return StringUtil.getPercent(processed,allnum);
    }

	public void setProcessed(int processed) {
		this.processed = processed;
	}

	public int getUndispose() {
		return undispose;
	}

	public void setUndispose(int undispose) {
		this.undispose = undispose;
	}

	public int getAllnum() {
		return allnum;
	}

	public void setAllnum(int allnum) {
		this.allnum = allnum;
	}

    public void setCharPieStatistics(Context mContext,PieChart chartStatistics){
        chartStatistics.setUsePercentValues(false);
        chartStatistics.getDescription().setEnabled(false);
        chartStatistics.setExtraOffsets(5, 3, 5, 5);

//        chartStatistics.setDragDecelerationFrictionCoef(0.95f);
//        chartStatistics.setCenterTextTypeface(mTfLight);
//        chartStatistics.setCenterText(generateCenterSpannableText());

//        chartStatistics.setDrawHoleEnabled(true);
//        chartStatistics.setHoleColor(Color.WHITE);

//        chartStatistics.setTransparentCircleColor(Color.WHITE);
//        chartStatistics.setTransparentCircleAlpha(110);
        chartStatistics.setHoleRadius(80f);
        chartStatistics.setTransparentCircleRadius(20f);

        chartStatistics.setDrawCenterText(true);
        chartStatistics.setRotationAngle(270);
        // enable rotation of the chart by touch
//        chartStatistics.setRotationEnabled(true);
//        chartStatistics.setHighlightPerTapEnabled(true);
        // chartStatistics.setUnit(" €");
        // chartStatistics.setDrawUnitsInChart(true);
        // add a selection listener
//        chartStatistics.setOnChartValueSelectedListener(this);

        setCharPieStatisticsData(mContext,chartStatistics);

        chartStatistics.animateY(1400, Easing.getEasingFunctionFromOption(Easing.EasingOption.EaseInOutElastic));
        // chartStatistics.spin(2000, 0, 360);

        Legend l = chartStatistics.getLegend();
        l.setEnabled(false);
//        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
//        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
//        l.setOrientation(Legend.LegendOrientation.VERTICAL);
//        l.setDrawInside(false);
//        l.setXEntrySpace(7f);
//        l.setYEntrySpace(0f);
//        l.setYOffset(0f);
//
//        // entry label styling
//        chartStatistics.setEntryLabelColor(Color.WHITE);
//        chartStatistics.setEntryLabelTextSize(12f);
//

    }

    void setCharPieStatisticsData(Context mContext,PieChart chartStatistics){

        SpannableString s = new SpannableString("完成率\n"+getProcessPercentStr());
        s.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.base_text_grey)), 0, 3, 0);
        s.setSpan(new RelativeSizeSpan(0.8f), 0, 3, 0);
//        s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0);
        s.setSpan(new ForegroundColorSpan(Color.BLACK), 3, s.length(), 0);
        s.setSpan(new RelativeSizeSpan(1.3f), 3, s.length(), 0);
//        s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0);
//        s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0);
        chartStatistics.setCenterText(s);


        ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
        // NOTE: The order of the entries when being added to the entries array determines their position around the center of
        // the chart.
//        entries.add(new PieEntry(25,""));
//        entries.add(new PieEntry(75,""));
        entries.add(new PieEntry(getProcessed(),""));
        entries.add(new PieEntry(getUndispose(),""));

        PieDataSet dataSet = new PieDataSet(entries, "statistics");
        dataSet.setDrawIcons(false);
//        dataSet.setSliceSpace(3f);
        dataSet.setIconsOffset(new MPPointF(0, 40));
        dataSet.setSelectionShift(5f);
        ArrayList<Integer> colors = new ArrayList<Integer>();
        colors.add(mContext.getResources().getColor(R.color.char_pie_color_blue));
        colors.add(mContext.getResources().getColor(R.color.char_pie_color_grey));
        colors.add(ColorTemplate.getHoloBlue());

        dataSet.setColors(colors);
        //dataSet.setSelectionShift(0f);

        PieData data = new PieData(dataSet);
        data.setDrawValues(false);
//        data.setValueFormatter(new PercentFormatter());
//        data.setValueTextSize(11f);
//        data.setValueTextColor(Color.WHITE);
        chartStatistics.setData(data);

        // undo all highlights
        chartStatistics.highlightValues(null);

        chartStatistics.invalidate();
    }
}
